/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication1;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author new
 */
public class FileFormatHandler {

    public String asmToRes(String fileName) throws FileNotFoundException, IOException{
        Pattern pattern;
        Matcher matcher;
        pattern = Pattern.compile("\\w*(?=.asm)",Pattern.CASE_INSENSITIVE);
        matcher =pattern.matcher(fileName);
        if(!matcher.find()){
            System.out.println(fileName + "is not a asm file.");
            System.exit(0);
        }
        String destFile = new String(matcher.group()+".res");

        FileHandler fh = new FileHandler();
        BufferedReader br = fh.openReadFile(fileName);
        BufferedWriter bw = fh.openWriteFile(destFile);
        
        String tempStr = br.readLine();
        while(tempStr != null){
            bw.append(tempStr);
            bw.newLine();
            tempStr = br.readLine();
        }

        fh.closeReadFile(br);
        fh.closeWriteFile(bw);

        return destFile;
    }

    public int toUpperCase(String fileName) throws FileNotFoundException, IOException{
        String temp = "UpperCaseTemp";
        FileHandler fh = new FileHandler();
        BufferedReader br = fh.openReadFile(fileName);
        BufferedWriter bw = fh.openWriteFile(temp);

        String tempStr = br.readLine();
        while(tempStr != null){
            bw.append(tempStr.toUpperCase());
            bw.newLine();
            tempStr = br.readLine();
        }

        fh.closeReadFile(br);
        fh.closeWriteFile(bw);
        System.gc();
        fh.replaceFile(temp, fileName);

        return 0;
    }

}
